package cn.lili.modules.payment.serviceimpl;


import cn.lili.common.security.context.UserContext;
import cn.lili.modules.payment.entity.dos.SubMerchantInformation;
import cn.lili.modules.payment.entity.enums.PaymentMethodEnum;
import cn.lili.modules.payment.mapper.SubMerchantInformationMapper;
import cn.lili.modules.payment.service.SubMerchantInformationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


/**
 * 子商户业务
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/3/28 14:33
 */

@Service
public class SubMerchantInformationServiceImpl extends ServiceImpl<SubMerchantInformationMapper, SubMerchantInformation> implements SubMerchantInformationService {


    @Override
    public SubMerchantInformation getSubMerchantInformation(PaymentMethodEnum paymentMethod) {
        return getSubMerchantInformationByUserId(UserContext.getCurrentUser().getExtendId(), paymentMethod);
    }

    @Override
    public SubMerchantInformation getSubMerchantInformationByUserId(String userId, PaymentMethodEnum paymentMethod) {
        LambdaQueryWrapper<SubMerchantInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubMerchantInformation::getPaymentMethod, paymentMethod);
        queryWrapper.eq(SubMerchantInformation::getUserId, userId);
        return this.getOne(queryWrapper, false);
    }

    @Override
    public SubMerchantInformation setSubMerchantId(PaymentMethodEnum paymentMethod, String subMerchantId) {
        SubMerchantInformation subMerchantInformation = getSubMerchantInformation(paymentMethod);
        if (subMerchantInformation == null) {
            subMerchantInformation = new SubMerchantInformation();
        }
        subMerchantInformation.setUserId(UserContext.getCurrentUser().getExtendId());
        subMerchantInformation.setPaymentMethod(paymentMethod.name());
        subMerchantInformation.setSubMerchantId(subMerchantId);
        this.saveOrUpdate(subMerchantInformation);
        return subMerchantInformation;
    }

    @Override
    public SubMerchantInformation setSubMerchantIdByUserId(String userId, PaymentMethodEnum paymentMethod, String subMerchantId) {
        SubMerchantInformation subMerchantInformation = getSubMerchantInformationByUserId(userId, paymentMethod);
        if (subMerchantInformation == null) {
            subMerchantInformation = new SubMerchantInformation();
        }
        subMerchantInformation.setUserId(userId);
        subMerchantInformation.setPaymentMethod(paymentMethod.name());
        subMerchantInformation.setSubMerchantId(subMerchantId);
        this.saveOrUpdate(subMerchantInformation);
        return subMerchantInformation;
    }
}